package com.yenlien.traceyun.business.controller;


import com.yenlien.traceyun.business.entity.Technology;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.TechnologyService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/6/11.
 */
@RestController
@RequestMapping("/technology")
public class TechnologyController extends BaseController {
    @Autowired
    private TechnologyService technologyService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Technology technology) {
        technology.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(technologyService.findByList(technology));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Technology technology, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        technology.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        technologyService.save(technology, files);
        return success("success");
    }

    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@RequestBody Technology technology) throws Exception {
        technology.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        technologyService.save2(technology);
        return success("success");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(Technology technology, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        technologyService.update(technology, files);
        return success("success");
    }

    @RequestMapping(value = "/update2", method = RequestMethod.POST)
    public Object update2(@RequestBody Technology technology) throws Exception {
        technologyService.update2(technology);
        return success("success");
    }

    @RequestMapping(value = "/findByProductName/{name}", method = RequestMethod.GET)
    public Object findByProductName(@PathVariable String name) {
        return success(technologyService.findByProductName(name));
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(technologyService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        technologyService.delete(id);
        return success("success");
    }

    @RequestMapping(value = "/findByAll", method = RequestMethod.GET)
    public Object findByAll() {
        return success(technologyService.findAll(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    // 导入工艺工序


}
